package com.ruoyi.project.ypjxc.controller;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.ypjxc.domain.Supplier;
import com.ruoyi.project.ypjxc.service.YpjxcSupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/ypjxc/supplier")
public class YpjxcSupplierController extends BaseController {
    @Autowired
    private YpjxcSupplierService supplierService;
    /**
     * 获取供应商列表
     */
    @PreAuthorize("@ss.hasPermi('ypjxc:supplier:list')")
    @GetMapping("/list")
    public TableDataInfo list(Supplier supplier) {
        startPage();
        List<Supplier> list = supplierService.selectSupplierList(supplier);
        return getDataTable(list);
    }

    /**
     * 新增供应商
     */
    @PreAuthorize("@ss.hasPermi('ypjxc:supplier:add')")
    @Log(title = "供应商管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Supplier supplier) {
        if (supplier==null){
            return AjaxResult.error("信息不能为空");
        }
        supplier.setCreateBy(SecurityUtils.getUsername());
        return toAjax(supplierService.insertSupplier(supplier));
    }

    /**
     * 修改供应商
     */
    @PreAuthorize("@ss.hasPermi('ypjxc:supplier:edit')")
    @Log(title = "供应商管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Supplier supplier)
    {
        if (supplier==null){
            return AjaxResult.error("信息不能为空");
        }
        supplier.setCreateBy(SecurityUtils.getUsername());
        return toAjax(supplierService.updateSupplier(supplier));
    }

    /**
     * 根据id查询
     * @param supplierId
     * @return
     */
    @PreAuthorize("@ss.hasPermi('ypjxc:supplier:query')")
    @GetMapping(value = "/{supplierId}")
    public AjaxResult getInfo(@PathVariable Long supplierId)
    {
        return AjaxResult.success(supplierService.selectSupplierById(supplierId));
    }

    @PreAuthorize("@ss.hasPermi('ypjxc:supplier:remove')")
    @Log(title = "供应商管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{supplierIds}")
    public AjaxResult delete(@PathVariable Long[] supplierIds){
        return toAjax(supplierService.deleteSupplierByIds(supplierIds));
    }


}
